// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

let db = cloud.database();
const _ = db.command;
const $ = db.command.aggregate;


// 云函数入口函数
exports.main = async (event, context) => {
  // return await db.collection('diary').get()
  const countResult = await db.collection('diary').count();//集合记录总数
  const total = countResult.total
  let pageInex = event.pageInex ? event.pageInex : 1;//第一页
  let pageSize = event.pageSize ? event.pageSize : 6;//每页条数
  // 计算需分几次取
  const totalPage = Math.ceil(total / pageSize)
  let skip = (pageInex - 1) * pageSize;
  let list=[];
  let diaryList =await db.collection('diary').aggregate()
    .sort({ time: -1 })//从大到小排序
    .skip(skip)//tiaogo条数
    .limit(pageSize)//输出条数
    .end()
  list = diaryList.list;
  return { list, total, totalPage}
}